package com.ks.app.navigate.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.persistence.Query;
import org.springframework.stereotype.Service;
import com.ks.app.admin.hibernate.KsOperaterLog;
import com.ks.app.admin.service.KsoperatelogService;
import com.ks.app.admin.service.impl.BaseServiceImpl;
import com.ks.app.admin.util.Pager;
import com.ks.app.navigate.hibernate.KsNavigate;
import com.ks.app.navigate.hibernate.KsWebsite;
import com.ks.app.navigate.service.KsNavigateService;
import com.ks.app.navigate.service.KsWebsiteService;


@Service("ksNavigateService")
public class KsNavigateServiceImpl extends BaseServiceImpl implements KsNavigateService{
	
	@Resource(name="ksWebsiteService")
	private KsWebsiteService webService;
	
	@Resource(name="ksoperatelogService")
	private KsoperatelogService logService;
	
	@SuppressWarnings("unchecked")
	@Override
	/**
	 * 查找所有上线的导航条
	 */
	public List<KsNavigate> findValidKsNavigate() {
		
		String hql = "from KsNavigate where status=2 order by ordernum desc";
		return em.createQuery(hql).getResultList();
	}

	/**
	 * 获取导航条列表
	 * parentid 上级导航条主键
	 * status   上线状态 1 下线，2 上线
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<KsNavigate> findKsNavigateList(String fname,long parentid, int status,Pager pager) {
		
		String sql  = " from KsNavigate where 1=1 ";
		String csql = " select count(*) from KsNavigate where 1=1";
		
		if(!"".equals(fname)){
			sql  = sql + "  and instr(name,?)>0";
			csql = csql + "  and instr(name,?)>0";
		}
		if (parentid != -1) {
			sql  = sql + "  and parentid="+parentid;
			csql = csql + "  and parentid="+parentid;
		}
		
		if(status != 0){
			sql  = sql +" and status="+status;
			csql = csql +" and status="+status;
		}
		
		sql  = sql +" order by ordernum desc";
	
		Query query = em.createQuery(sql);
		Query cquery = em.createQuery(csql);
		
		if(!"".equals(fname)){
			query.setParameter(1, fname);
			cquery.setParameter(1, fname);
		}
		
		query.setMaxResults(pager.getCntOfPage());
		query.setFirstResult((pager.getCurPage() - 1) * pager.getCntOfPage());
		pager.setTotoalCnt(Integer.parseInt(String.valueOf(cquery.getSingleResult())));
		
		List<KsNavigate> list = query.getResultList();
		
		return list;

	}

	@Override
	/**
	 * 新增或修改导航条
	 */
	public void saveKsNavigate(KsNavigate nav) {
		em.merge(nav);
	}

	@Override
	/**
	 * 通过主键获取导航条
	 */
	public KsNavigate findKsNavigateById(long id) {
		
		KsNavigate nav = em.find(KsNavigate.class, id);
		return nav;
	}
	
	/**
	 * 所有导航条信息
	 */
	@Override
	public Map<String, Object> findNavigateWebsite() {
		
		List<KsWebsite>  wlist = webService.findOnlineWeblist();      //上线站点
		List<KsNavigate> nlist = findValidKsNavigate();               //有效排序导航条
		List<KsWebsite>  rlist = webService.findRecommendWebList();   //推荐站点
	
		
		List<Map<String,Object>> trlist = new ArrayList<Map<String,Object>>();  //导航条一级节点
		for(KsNavigate ng : nlist){
			if(ng.getParentid()==0){
				
				Map<String,Object> map0  = new HashMap<String,Object>();
				map0.put("id", ng.getId());
				map0.put("name", ng.getName());
				trlist.add(map0);
			}
		}
		
		for(Map<String,Object> m : trlist){                       
			
			long typeid = Long.parseLong(String.valueOf(m.get("id")));
			
			List<Map<String,Object>> nwlist = new ArrayList<Map<String,Object>>();  //导航条二级节点
			List<Map<String,Object>>  recommendList = new ArrayList<Map<String,Object>>();    //一级导航条推荐站点
		
			for(KsNavigate ng : nlist){    
				
				if(ng.getParentid()==typeid){
					
					Map<String,Object> map1  = new HashMap<String,Object>();
					List<Map<String,Object>>    list1 = new ArrayList<Map<String,Object>>();
					int num =0;
					for(KsWebsite w1 : wlist){
						if(w1.getNavigateid()==ng.getId()){
							Map<String,Object> wm = new HashMap<String,Object>();
							wm.put("name", w1.getName());
							wm.put("url", w1.getUrl());
							wm.put("logo", w1.getLogo());							
							list1.add(wm);
							num ++;
						}
						if(num>3){
							break;
						}
					}	
					map1.put("id", ng.getId());
					map1.put("name", ng.getName());
					map1.put("type", ng.getParentid());
					map1.put("list", list1);
					nwlist.add(map1);
					
					
					for(KsWebsite ws : rlist){
						if(ws.getNavigateid()==ng.getId()){
							Map<String,Object> wm = new HashMap<String,Object>();
							wm.put("name", ws.getName());
							wm.put("url", ws.getUrl());
							wm.put("logo", ws.getLogo());
							recommendList.add(wm);
						}
					}
				}
				
			}
			m.put("list", nwlist);
			m.put("recommend", recommendList);
			
		}
		
		Map<String,Object> rmap = new HashMap<String,Object>();
		rmap.put("tree", trlist);
		return rmap;
	}

	
	
	/**
	 * 获取所有导航条
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<KsNavigate> findParentKsNavigate() {
		String hql = "from KsNavigate where  parentid=0 order by ordernum desc";
		return em.createQuery(hql).getResultList();
		
	}

	/**
	 * 删除导航条，连同其下站点
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void deleteKsNavigates(String ids,String user) {
		
		String webhql = "delete KsWebsite where navigateid in ("+ids+")";
		em.createQuery(webhql).executeUpdate();
		
		String shql = "from KsNavigate where id in("+ids+")";
		List<KsNavigate> list = em.createQuery(shql).getResultList();
		
		String content = "";
		for(KsNavigate n : list){
			content += n.getName()+",";
			em.remove(n);
		}
		
		KsOperaterLog log = new KsOperaterLog();
		log.setLogtime(new Date());
		log.setLogtype(1);
		log.setOpename("删除导航条");
		log.setOperater(user);
		log.setStatus(1);
		log.setContent(content);
		
		logService.save(log);
	
	}

	/**
	 * 叶子导航条
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<KsNavigate> findLeafKsNavigate() {
		
		String hql = "from KsNavigate where parentid>0 order by ordernum desc";
		return em.createQuery(hql).getResultList();
	}
	
	
	
	/**
	 * 按推荐指数排序
	 * @param list
	 *//*
	@SuppressWarnings("unchecked")
	public void sortWebList(List<Map<String,Object>>  list){
		
		Collections.sort(list, new Comparator(){
			@Override
			public int compare(Object o1, Object o2) {
				int r1 = Integer.parseInt(String.valueOf(((Map<String,Object>)o1).get("ordernum")));
				int r2 = Integer.parseInt(String.valueOf(((Map<String,Object>)o2).get("ordernum")));	
				return r2-r1;
			}
		});
		
	}
	
	*//**
	 * 按推荐指数排序
	 * @param list
	 *//*
	@SuppressWarnings("unchecked")
	public void sortNavList(List<KsNavigate>  list){
		
		Collections.sort(list, new Comparator(){
			@Override
			public int compare(Object o1, Object o2) {
				int r1 = ((KsNavigate)o1).getOrdernum();
				int r2 = ((KsNavigate)o2).getOrdernum();	
				return r2-r1;
			}
		});
		
	}*/

}
